package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Clothes;
import com.skyc.entity.User;
import com.skyc.service.ClothesService;
import com.skyc.service.impl.ClothesServiceImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/ToItemShowServlet")
public class ToItemShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		ServletContext application = getServletContext();
		PrintWriter out = response.getWriter();
		ClothesService cs = new ClothesServiceImpl();
		String temp = request.getParameter("temp");
		if (temp.equals("one")) {
			String ccid = request.getParameter("ccid");
			String color = "白色";
			String size = "S";
			String cid = cs.cidqueryByIdSizeColor(ccid, size, color) + "";
			List<Clothes> list = cs.listqueryBySalesCcid();
			for (Clothes c : list) {
				c.setCimgurl(c.getCimgurl().substring(0, c.getCimgurl().indexOf('/')));
			}

			int kucun = cs.kucunqueryByIDSizeColor(ccid, size, color);// 该件衣服默认的白色 S码的库存
			int csales = cs.salesqueryByccid(ccid);// 衣服的总销量
			Clothes c = cs.clothesqueryByCcid(ccid);// 通过CCID得到衣服的基本信息
			String img[] = c.getCimgurl().split("/");
			String img2[] = c.getCimgurl().split("/");
			for (int i = 0; i < img.length; i++) {
				img2[i] = img2[i].replace("s", "b");// 替换成大图
			}
			session.setAttribute("cid", cid);
			session.setAttribute("list", list);
			session.setAttribute("clothes", c);
			request.setAttribute("kucun", kucun);
			request.setAttribute("img", img);
			request.setAttribute("img2", img2);
			request.setAttribute("imglength", img.length - 1);
			request.setAttribute("csales", csales);
			request.getRequestDispatcher("item_show.jsp").forward(request, response);
		} else if (temp.equals("two")) {
			String color = request.getParameter("color");
			String size = request.getParameter("size");
			String ccid = request.getParameter("ccid");
			ccid = ccid.substring(1);
			String cid = cs.cidqueryByIdSizeColor(ccid, size, color) + "";
			int kucun = cs.kucunqueryByIDSizeColor(ccid, size, color);
			session.setAttribute("cid", cid);
			request.setAttribute("kucun", kucun);
			out.print(kucun + "/" + cid);
		}
		out.flush();
		out.close();
	}
}
